<template>
	<!-- 不安全行为录入 -->
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :label-width="'100'" :model="valiFormData" :label-position="'top'" :formItem="formData"></my-forms>
			<u-button type="primary" shape="circle" text="保存提交" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
import { computed, reactive, ref } from 'vue';
import { addViolation } from '@/api/hidden-danger.js';
import { listRiskPointManagement } from '@/api/riskcontrol.js';

// 表单数据
const formData = computed(() => {
	return [
		{
			label: '不安全行为描述',
			required: true,
			prop: 'vioFactDesc',
			type: 'textarea'
		},
		{
			label: '不安全产生因素',
			prop: 'threeViolateFactor',
			itemType: 'select',
			type: 'HIDDEN_DANGER_FACTOR'
		},
		{
			itemType: 'select',
			list: checkList.value,
			label: '违章地点',
			required: true,
			prop: 'riskLocationId'
		},
		{
			itemType: 'cascader',
			type: 'GETdeptTree',
			label: '违章单位',
			required: true,
			prop: 'vioDeptId'
		},
		{
			label: '违章人员',
			required: true,
			prop: 'vioUserName'
		},
		{
			itemType: 'date',
			type: 'date',
			label: '违章时间',
			required: true,
			prop: 'vioDate'
		},
		{
			itemType: 'select',
			type: 'GETvioLevel',
			label: '不安全行为等级',
			required: true,
			prop: 'vioLevel'
		},
		// {
		// 	itemType: 'select',
		// 	type: 'GETspecialtyKey',
		// 	label: '不安全行为专业',
		// 	required: true,
		// 	prop: 'vioCategory'
		// },
		{
			itemType: 'select',
			type: 'GETvioType',
			label: '不安全行为类别',
			required: true,
			prop: 'vioType'
		},
		{
			itemType: 'cascader',
			type: 'GETdeptTree',
			label: '查处单位',
			required: true,
			prop: 'findDeptId'
		},
		{
			label: '发现人',
			required: true,
			prop: 'stopUserName'
		},
		{
			label: '罚款（元）',
			prop: 'vioFineMoney',
			type: 'number',
			maxlength: 9
		},
		{
			label: '主要原因',
			required: true,
			prop: 'mainReason',
			type: 'textarea'
		},
		{
			label: '处理结果',
			required: true,
			prop: 'threatResult',
			type: 'textarea'
		},
		{
			itemType: 'image',
			label: '图片上传',
			prop: 'picturePaths'
		},
		{
			itemType: 'video',
			label: '视频上传',
			prop: 'videoPaths'
		}
	].filter((i) => i);
});
const valiForm = ref(null);
const valiFormData = reactive({
	type: '0'
});

// 保存提交
const submit = () => {
	valiForm.value.$refs.refMyForm
		.validate()
		.then(async (res) => {
			const { code, msg } = await addViolation(valiFormData);
			if (code == 200) {
				uni.showToast({
					icon: 'success',
					title: `录入成功`
				});
				uni.navigateBack({
					delta: 1 // 返回的页面数，如果是1则返回上一页
				});
			} else {
				uni.showToast({
					title: msg
				});
			}
		})
		.catch((err) => {
			console.log('err', err);
			uni.showToast({
				icon: 'error',
				title: err[0].errorMessage
			});
		});
};
// 获取检查地点列表
let checkList = ref([]);
const getCheckPonit = async () => {
	const { data } = await listRiskPointManagement();
	checkList.value = data.map((item) => ({
		dictLabel: item.riskLocationName,
		dictValue: item.id
	}));
};
getCheckPonit();
</script>
<style lang="scss" scoped></style>
